package com.yijiaw.dao;

import com.yijiaw.domain.Part;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Part entities.
 * 
 */
@Repository("PartDAO")
@Transactional
public class PartDAOImpl extends AbstractJpaDao<Part> implements PartDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Part.class }));

	/**
	 * EntityManager injected by Spring for persistence unit yijiaw
	 *
	 */
	@PersistenceContext(unitName = "yijiaw")
	private EntityManager entityManager;

	/**
	 * Instantiates a new PartDAOImpl
	 *
	 */
	public PartDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findPartByImageContaining
	 *
	 */
	@Transactional
	public Set<Part> findPartByImageContaining(String image) throws DataAccessException {

		return findPartByImageContaining(image, -1, -1);
	}

	/**
	 * JPQL Query - findPartByImageContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findPartByImageContaining(String image, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPartByImageContaining", startResult, maxRows, image);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * JPQL Query - findPartByRemark
	 *
	 */
	@Transactional
	public Set<Part> findPartByRemark(String remark) throws DataAccessException {

		return findPartByRemark(remark, -1, -1);
	}

	/**
	 * JPQL Query - findPartByRemark
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findPartByRemark(String remark, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPartByRemark", startResult, maxRows, remark);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * JPQL Query - findPartByNameContaining
	 *
	 */
	@Transactional
	public Set<Part> findPartByNameContaining(String name) throws DataAccessException {

		return findPartByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findPartByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findPartByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPartByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * JPQL Query - findPartByPrimaryKey
	 *
	 */
	@Transactional
	public Part findPartByPrimaryKey(Integer id) throws DataAccessException {

		return findPartByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findPartByPrimaryKey
	 *
	 */

	@Transactional
	public Part findPartByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPartByPrimaryKey", startResult, maxRows, id);
			return (com.yijiaw.domain.Part) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPartByName
	 *
	 */
	@Transactional
	public Set<Part> findPartByName(String name) throws DataAccessException {

		return findPartByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findPartByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findPartByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPartByName", startResult, maxRows, name);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * JPQL Query - findPartById
	 *
	 */
	@Transactional
	public Part findPartById(Integer id) throws DataAccessException {

		return findPartById(id, -1, -1);
	}

	/**
	 * JPQL Query - findPartById
	 *
	 */

	@Transactional
	public Part findPartById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPartById", startResult, maxRows, id);
			return (com.yijiaw.domain.Part) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findPartByImage
	 *
	 */
	@Transactional
	public Set<Part> findPartByImage(String image) throws DataAccessException {

		return findPartByImage(image, -1, -1);
	}

	/**
	 * JPQL Query - findPartByImage
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findPartByImage(String image, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPartByImage", startResult, maxRows, image);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllParts
	 *
	 */
	@Transactional
	public Set<Part> findAllParts() throws DataAccessException {

		return findAllParts(-1, -1);
	}

	/**
	 * JPQL Query - findAllParts
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Part> findAllParts(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllParts", startResult, maxRows);
		return new LinkedHashSet<Part>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Part entity) {
		return true;
	}
}
